package com.easou.ps.main;

import java.util.ArrayList;
import java.util.List;

import com.easou.ps.master.channel.RequestChannel;
import com.easou.ps.master.knowledge.KnowledgeUtils;
import com.easou.ps.master.knowledge.RequestKnowledge;
import com.easou.ps.master.lbs.RequestLBS;
import com.easou.ps.master.query.qs.QSProxy;
import com.easou.ps.master.query.qs.QSProxyFactory;
import com.easou.ps.rpc.temp.MainSearchRPC;
import com.easou.ps.sort.InsertChannelResult;
import com.easou.ps.thrift.ks.KnowledgeSearchResponse;
import com.easou.ps.thrift.qs.QSRequest;
import com.easou.ps.thrift.qs.QSResult;
import com.easou.ps.thrift.qs.Query;
import com.easou.ps.thrift.qs.QueryRewriteType;
import com.easou.ps.thrift.qs.QueryType;
import com.easou.ps.thrift.search.mainsearch.MainSearchResponse;
import com.easou.ps.utils.ConvsUtils;
import com.easou.ps.webmaster.SearchRequest;
import com.easou.ps.webmaster.SearchResponse;
import com.easou.ps.webmaster.SearchResult;

public class MiscSearcher {
	public SearchResponse toSearch(SearchRequest request){
		SearchResponse response = new SearchResponse();
		QSResult qsResult = getQSResult(request.getQ());
		//mainSearch
		MainSearchResponse mainResponse = MainSearchRPC.toSearch(request,qsResult);
		response = ConvsUtils.convMainSearchResponse2SearchResponse(request, mainResponse, qsResult);
		//截取出mainSearch的显示结果
		resetSearchResponse(request, response);
		
		//LBS-OK
		SearchResult lbsSearchResult = RequestLBS.toGet(request, qsResult);
		if(lbsSearchResult!=null){
			response.getResults().add(0, lbsSearchResult);
		}else{
			System.out.println("lbs result is null.");
		}
		
		//外部整合-OK
		List<SearchResult> channelSearchResults = RequestChannel.toGet(request,qsResult);
		
		
		//ks
		boolean is_p_m = false;//是否是人物类或者视频类
		Query query = qsResult.getQuerys().get(QueryRewriteType.Normal);
		List<QueryType> queryTypes = query.getTypes();
		for (int i = 0; i < queryTypes.size(); i++) {
			if(queryTypes.get(i).getType()==83 || queryTypes.get(i).getType() == 204){
				is_p_m = true;
			}
		}
		if(is_p_m){//人物类和视频类的排序方案
			System.out.println("is_p_m == true");
		}else{//和以前master一样的排序方案
			//外部整合
			System.out.println("is_p_m == false");
			InsertChannelResult.todo(response,channelSearchResults);
			
			//ks
			RequestKnowledge requestKnowledge = new RequestKnowledge();
			boolean hasResult = requestKnowledge.toGet(request, qsResult);
			System.out.println("hasResult:"+hasResult);
			if(hasResult){
				List<KnowledgeSearchResponse> ksResults = requestKnowledge.toGetResults();
				KnowledgeUtils.toAddKnowledge2Response(response, request, ksResults);
			}
		}
		
		return response;
	}
	
	private void resetSearchResponse(SearchRequest request, SearchResponse response) {
		List<SearchResult> resultList = new ArrayList<SearchResult>();
		int startIndex = (request.getPageNum() - 1) * request.getPageSize();
		int endIndex = startIndex + request.getPageSize();
		for (int n = startIndex; n < endIndex && n < response.getResultsSize(); n++) {
			resultList.add(response.getResults().get(n));
		}
		response.setResults(resultList);
	}
	
	public static QSResult getQSResult(String query){
		QSRequest qsReq = new QSRequest();
		qsReq.setQ(query);
		qsReq.setSeqNo(0L);
		qsReq.setDebug(false);
		qsReq.setUa("Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
		qsReq.setCid("");
		qsReq.setWordType(0);
		
		QSProxy proxy = QSProxyFactory.getProxy();
		if(proxy!=null){
			QSResult qsResult = proxy.call(qsReq);
			return qsResult;
		}else{
			System.out.println("proxy == null");
			return null;
		}
		
	}
}
